package com.ruoyi.insurance.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.insurance.domain.ConcreteBug;
import com.ruoyi.insurance.mapper.EvaluateBeforeInsuranceMapper;
import com.ruoyi.insurance.domain.EvaluateBeforeInsurance;
import com.ruoyi.insurance.service.IEvaluateBeforeInsuranceService;

/**
 * 保前安全评测Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-09
 */
@Service
public class EvaluateBeforeInsuranceServiceImpl implements IEvaluateBeforeInsuranceService 
{
    @Autowired
    private EvaluateBeforeInsuranceMapper evaluateBeforeInsuranceMapper;

    /**
     * 查询保前安全评测
     * 
     * @param evaluateBeforeId 保前安全评测主键
     * @return 保前安全评测
     */
    @Override
    public EvaluateBeforeInsurance selectEvaluateBeforeInsuranceByEvaluateBeforeId(Long evaluateBeforeId)
    {
        return evaluateBeforeInsuranceMapper.selectEvaluateBeforeInsuranceByEvaluateBeforeId(evaluateBeforeId);
    }

    /**
     * 查询保前安全评测列表
     * 
     * @param evaluateBeforeInsurance 保前安全评测
     * @return 保前安全评测
     */
    @Override
    public List<EvaluateBeforeInsurance> selectEvaluateBeforeInsuranceList(EvaluateBeforeInsurance evaluateBeforeInsurance)
    {
        return evaluateBeforeInsuranceMapper.selectEvaluateBeforeInsuranceList(evaluateBeforeInsurance);
    }

    /**
     * 新增保前安全评测
     * 
     * @param evaluateBeforeInsurance 保前安全评测
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEvaluateBeforeInsurance(EvaluateBeforeInsurance evaluateBeforeInsurance)
    {
        int rows = evaluateBeforeInsuranceMapper.insertEvaluateBeforeInsurance(evaluateBeforeInsurance);
        insertConcreteBug(evaluateBeforeInsurance);
        return rows;
    }

    /**
     * 修改保前安全评测
     * 
     * @param evaluateBeforeInsurance 保前安全评测
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEvaluateBeforeInsurance(EvaluateBeforeInsurance evaluateBeforeInsurance)
    {
        evaluateBeforeInsuranceMapper.deleteConcreteBugByEvaluateBeforeInsuranceId(evaluateBeforeInsurance.getEvaluateBeforeId());
        insertConcreteBug(evaluateBeforeInsurance);
        return evaluateBeforeInsuranceMapper.updateEvaluateBeforeInsurance(evaluateBeforeInsurance);
    }

    /**
     * 批量删除保前安全评测
     * 
     * @param evaluateBeforeIds 需要删除的保前安全评测主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvaluateBeforeInsuranceByEvaluateBeforeIds(Long[] evaluateBeforeIds)
    {
        evaluateBeforeInsuranceMapper.deleteConcreteBugByEvaluateBeforeInsuranceIds(evaluateBeforeIds);
        return evaluateBeforeInsuranceMapper.deleteEvaluateBeforeInsuranceByEvaluateBeforeIds(evaluateBeforeIds);
    }

    /**
     * 删除保前安全评测信息
     * 
     * @param evaluateBeforeId 保前安全评测主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvaluateBeforeInsuranceByEvaluateBeforeId(Long evaluateBeforeId)
    {
        evaluateBeforeInsuranceMapper.deleteConcreteBugByEvaluateBeforeInsuranceId(evaluateBeforeId);
        return evaluateBeforeInsuranceMapper.deleteEvaluateBeforeInsuranceByEvaluateBeforeId(evaluateBeforeId);
    }

    /**
     * 新增详细漏洞信息
     * 
     * @param evaluateBeforeInsurance 保前安全评测对象
     */
    public void insertConcreteBug(EvaluateBeforeInsurance evaluateBeforeInsurance)
    {
        List<ConcreteBug> concreteBugList = evaluateBeforeInsurance.getConcreteBugList();
        Long evaluateBeforeId = evaluateBeforeInsurance.getEvaluateBeforeId();
        if (StringUtils.isNotNull(concreteBugList))
        {
            List<ConcreteBug> list = new ArrayList<ConcreteBug>();
            for (ConcreteBug concreteBug : concreteBugList)
            {
                concreteBug.setEvaluateBeforeInsuranceId(evaluateBeforeId);
                list.add(concreteBug);
            }
            if (list.size() > 0)
            {
                evaluateBeforeInsuranceMapper.batchConcreteBug(list);
            }
        }
    }
}
